<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="因子标题" prop="factorItemTitle">
        <el-input
          v-model="queryParams.factorItemTitle"
          placeholder="请输入因子标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="因子类别id" prop="parentCategoryId">
        <el-input
          v-model="queryParams.parentCategoryId"
          placeholder="请输入因子类别id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="因子类别标题" prop="parentCategoryTitle">
        <el-input
          v-model="queryParams.parentCategoryTitle"
          placeholder="请输入因子类别标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:item:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:item:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="因子id" align="center" prop="factorItemId" />-->
      <el-table-column label="因子标题" align="center" prop="factorItemTitle" />
      <el-table-column label="因子别名" align="center" prop="factorItemEng" />
      <!--<el-table-column label="因子类别id" align="center" prop="parentCategoryId" />-->
      <el-table-column label="因子类别标题" align="center" prop="parentCategoryTitle" />
      <!--<el-table-column label="用户id" align="center" prop="userId" />-->
      <!--<el-table-column label="算术表达式" align="center" prop="longText" />-->
      <!--<el-table-column label="xml字符串" align="center" prop="xmlText" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自定义因子对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!--部门数据-->

        <el-form-item label="因子标题" prop="factorItemTitle">
          <el-input v-model="form.factorItemTitle" placeholder="请输入因子标题" />
        </el-form-item>
        <el-form-item label="因子英文别名" prop="factorItemTitle">
          <el-input v-model="form.factorItemEng" placeholder="请输入因子英文名称" />
        </el-form-item>

        <!--<el-form-item label="因子类别id" prop="parentCategoryId">
          <el-input v-model="form.parentCategoryId" placeholder="请输入因子类别id" />
        </el-form-item>-->
        <el-form-item label="因子选择" prop="factorItemTitle">
          <el-col :span="4" :xs="24">
            <div class="head-container" style="width: 300px">
              <el-tree
                :data="deptOptions"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                :ref="tree"
                :default-expand-all="false"
                @node-click="handleNodeClick1"
              />
            </div>
          </el-col>
        </el-form-item>

        <!--<el-form-item label="表达式样式" prop="menuType">-->
          <!--<el-radio-group v-model="form.menuType">-->
            <!--<el-radio  label="F" >单值</el-radio>-->
            <!--<el-radio  label="C">多值</el-radio>-->
            <!--<el-radio  label="D">自定义</el-radio>-->
          <!--</el-radio-group>-->
        <!--</el-form-item>-->

        <el-form-item label="算术表达式" prop="longText">
          <!--<el-col v-if="form.menuType == 'C'">-->
          <!--<el-input v-model="form.longText1" type="text" :aria-readonly="true" placeholder="请选择因子" style="width: 15%;cursor:not-allowed;"/>-->
              <!--<el-select v-model="form.postIds1"  placeholder="请选择运算符" style="width: 30%">-->
                <!--<el-option-->
                  <!--v-for="item in factors"-->
                  <!--:key="item.value"-->
                  <!--:label="item.label"-->
                  <!--:value="item.value"-->
                <!--&gt;</el-option>-->
              <!--</el-select>-->


          <!--<el-input v-model="form.longText2" type="text" readonly="true" placeholder="请选择因子" style="width: 15%;cursor:not-allowed;" />-->
          <!--</el-col>-->
          <!--<el-col v-if="form.menuType == 'F'">-->
            <!--<el-select v-model="form.postIds2"  placeholder="请选择运算符" style="width: 30%">-->
              <!--<el-option-->
                <!--v-for="item in factors"-->
                <!--:key="item.value"-->
                <!--:label="item.label"-->
                <!--:value="item.value"-->
              <!--&gt;</el-option>-->
            <!--</el-select>-->
            <!--<el-input v-model="form.longText3" type="text" readonly="true" placeholder="请选择因子" style="width: 15%;cursor:not-allowed;" />-->
          <!--</el-col>-->
          <el-col >
            <el-input v-model="form.longText" type="textarea" placeholder="请输入内容" />
          </el-col>
            <el-button id="reset" style="width: 15%" @click="resetItem">重置</el-button>

        </el-form-item>



        <!--<el-form-item label="xml字符串" prop="xmlText">-->
          <!--<el-input v-model="form.xmlText" type="textarea" placeholder="请输入内容" />-->
        <!--</el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listItem, getItem, delItem, addItem, updateItem,treeselect,listFactor } from "@/api/system/item";

export default {
  name: "Item",
  data() {
    return {
      selected:true,//默认的字体样式
      // isExpand:false,
      // 遮罩层
      loading: true,
      menuType: "F",
      // 选中数组
      ids: [],
      factors: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // defaultExpandLevel: 0,
      //因子的树结构
      deptOptions:[],
      // 总条数
      total: 0,
      // 自定义因子表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factorItemTitle: null,
        parentCategoryId: null,
        parentCategoryTitle: null,
        userId: null,
        longText: null,
        xmlText: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentCategoryId: [
          { required: true, message: "因子类别id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  activated () {
    this.getList();
  },
  created() {
    this.$forceUpdate();
    this.getTreeselect();
    this.getList();
    this.getFactor();
  },
  methods: {
    /** 查询自定义因子列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询自定义运算符列表 */
    getFactor() {
      listFactor().then(response => {
        this.factors = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        factorItemId: null,
        factorItemTitle: null,
        parentCategoryId: null,
        parentCategoryTitle: null,
        userId: null,
        menuType: "F",
        // longText1:null,
        // longText2:null,
        // longText3:null,
        factorItemEng:null,
        longText: null,
        xmlText: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置自定义因子的数据 */
    resetItem() {

      // this.form.longText1=null;
      // this.form.longText2=null;
      // this.form.longText3=null;
      this.form.longText=null;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询列表下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data
      })
    },

    /** 点击树形结构获取值 */
    handleNodeClick1(data) {
      if(this.form.longText!=null)
        this.form.longText = this.form.longText+data.label+"("+data.id+")";
      else
        this.form.longText = data.label+"("+data.id+")";

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.factorItemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加自定义因子";
      var url = '/strategy/useritem';
      // alert(url);
      this.$router.push(url);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      var url = '/strategy/useritem';
      const factorItemId = row.factorItemId || this.ids
      // alert(url);
      this.$router.push({path:url,query:{factorItemId:factorItemId}});

      //
      //
      //
      // this.reset();
      //
      // getItem(factorItemId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改自定义因子";
      // });
    },

    /** 单选的选中数据 */
    // choose() {
    //   $ = this.$;
    //   alert(S("#search_type").val);
    //   alert(1111);
    // },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.factorItemId != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const factorItemIds = row.factorItemId || this.ids;
      this.$modal.confirm('是否确认删除自定义因子编号为"' + factorItemIds + '"的数据项？').then(function() {
        return delItem(factorItemIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>



